package com.chen.dao.impl;


import com.chen.dao.UserDao;
import com.chen.entity.User;
import com.chen.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements UserDao {
    Connection conn = DBUtil.getConnection();
    QueryRunner qr = new QueryRunner();

    @Override
    public int save(User obj) {
        String sql = "INSERT INTO  tb_user(user_id,user_name,user_money,user_point,user_gender,user_birthday,user_phone,user_image,user_password) VALUES (?,?,?,?,?,?,?,?,?)";
        Object[] paras = {obj.getUser_id(), obj.getUser_name(), obj.getUser_money(), obj.getUser_point(), obj.getUser_gender(),
                obj.getUser_birthday(), obj.getUser_phone(), obj.getUser_image(), obj.getUser_password()};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(User obj) {
        String sql = "UPDATE  tb_user SET  user_name=?,user_money=?,user_point=?,user_gender=?,user_birthday=?,user_phone=?,user_image=?,user_password=? WHERE user_id=?";
        Object[] paras = {obj.getUser_name(), obj.getUser_money(), obj.getUser_point(), obj.getUser_gender(),
                obj.getUser_birthday(), obj.getUser_phone(), obj.getUser_image(), obj.getUser_password(), obj.getUser_id()};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(Object id) {
        String sql = "DELETE FROM tb_user WHERE user_id  = ? ";
        Object[] paras = {id};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public User findById(Object key) {
        String sql = "SELECT * FROM tb_user where user_id  = ?";
        Object[] paras = {key};
        try {
            User user = qr.query(conn, sql, paras, new BeanHandler<User>(User.class));
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> findAll() {
        String sql = "SELECT * FROM tb_user";
        Object[] paras = {};
        try {
            List<User> userList = qr.query(conn, sql, paras, new BeanListHandler<User>(User.class));
            return userList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> findSearch(String keyword) {
        String sql = "SELECT * FROM tb_user where user_name like concat('%',concat(?,'%'))";
        Object[] paras = {};
        try {
            List<User> userList = qr.query(conn, sql, paras, new BeanListHandler<User>(User.class));
            return userList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> findPage(int page, int pagesize) {
        String sql = "SELECT * FROM tb_user limit ?,?";
        Object[] paras = {(page - 1) * pagesize, pagesize};
        try {
            List<User> userList = qr.query(conn, sql, paras, new BeanListHandler<User>(User.class));
            return userList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long count() {
        String sql = "select count(*) from tb_user";
        Object[] paras = {};
        try {
            long nums = qr.query(conn, sql, paras, new ScalarHandler<Long>());
            return nums;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public User login(String name) {
        String sql = "SELECT * FROM tb_user where user_name  = ? or user_phone = ?";
        Object[] paras = {name, name};
        try {
            User user = qr.query(conn, sql, paras, new BeanHandler<User>(User.class));
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
